package com.cenwangkeji.resident.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.FrameMetrics;
import android.view.View;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.FamilyMemberBean;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.FamilyAdapter;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.activity.AppManager;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//家庭成员
public class FamilyActivity extends BaseActivity implements BaseQuickAdapter.OnItemLongClickListener {

    @BindView(R.id.rv)
    RecyclerView mRv;
    private FamilyAdapter mAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_family;
    }



    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initReycerlView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getdata();
    }

    private void initReycerlView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new FamilyAdapter(null);
        mAdapter.setOnItemLongClickListener(this);
        mRv.setAdapter(mAdapter);
    }

    @OnClick(R.id.iv_add)
    public void onViewClicked() {
        startActivity(new Intent(this,AddFamilyActivity.class));
    }

    private void getdata(){
        showLoading();
        Map<Object, Object> map = new HashMap<>();
        map.put("userId", User.getUserId());
        OkHttpUtils.post(Constant.family_member,map, FamilyMemberBean.class)
                .subscribe(new ObserverOnNext<FamilyMemberBean>() {
            @Override
            public void onNext(FamilyMemberBean bean) {
                if (bean.getData() !=null &&bean.getData().size()>0) {
                    mAdapter.setNewData(bean.getData());
                } else {
                    View emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
                    mAdapter.setNewData(null);
                    mAdapter.setEmptyView(emptyView);
                    MyToast.show(bean.getMsg());
                }
                dismissLoading();
            }
        });

    }


    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {

           showBackDialog(position);
        return true;
    }

    private void showBackDialog(final int p){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("删除成员");
        normalDialog.setMessage("是否删除该成员");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Observable.create(new ObservableOnSubscribe<SuccessBean>() {
                            @Override
                            public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                                Map<Object,Object> map = new HashMap<>();
                                map.put( Constant.token, User.getToken());
                                map.put( "mark",3);
                                map.put("userId",mAdapter.getData().get(p).getId());
                                String j = OkHttpUtils.post((new JSONObject(map).toString()), Constant.delete_family);
                                MyLogger.i(j);
                                Gson gson = new Gson();
                                SuccessBean successBean  = gson.fromJson(j, SuccessBean.class);
                                emitter.onNext(successBean);



                            }
                        }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
                            @Override
                            public void onNext(SuccessBean bean) {
                                if (bean.getCode()==0){
                                    MyToast.show("删除成功");
                                    getdata();
                                }else {
                                    MyToast.show(bean.getMsg());
                                }

                            }
                        });

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }
}
